package luke.ui.dialogs;

import luke.core.config.AppDataManager;
import luke.core.utils.LanguageManager; // 新增：导入LanguageManager

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * 捐赠对话框类
 * 用于显示捐赠信息和处理用户捐赠操作
 */
public class DonationDialog extends JDialog {
    private static final String PREF_DONATION_STATUS = "donation_status";
    private static AppDataManager appDataManager = AppDataManager.getInstance();
    private static LanguageManager languageManager = LanguageManager.getInstance(); // 新增：语言管理器
    
    private boolean donated = false;
    private JButton donateButton;
    private JButton closeButton;
    private String source; // 新增字段：记录调用来源（"menu" 或 "generate"）

    public DonationDialog(Frame parent, String source) {
        super(parent, languageManager.getString("dialog.donation.title"), true); // 模态对话框（国际化标题）
        this.source = source; // 记录调用来源
        setupUI();
        setupEventHandlers();
        pack();
        setResizable(false);
        setLocationRelativeTo(parent);
    }
    
    private void setupUI() {
        setLayout(new BorderLayout());
        
        // 创建主面板
        JPanel mainPanel = new JPanel();
        mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
        // 添加边框设置以恢复外部留白
        mainPanel.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
        
        // 添加标题
        JLabel titleLabel = new JLabel(languageManager.getString("dialog.donation.header"), SwingConstants.CENTER);
        titleLabel.setFont(titleLabel.getFont().deriveFont(16f).deriveFont(Font.BOLD));
        titleLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
        mainPanel.add(titleLabel);
        mainPanel.add(Box.createRigidArea(new Dimension(0, 15)));
        
        // 根据捐赠状态显示不同话术
        if (appDataManager.isDonated()) {
            // 已捐赠状态话术
            JLabel thankYouLabel = new JLabel(languageManager.getString("dialog.donation.thankyou"), SwingConstants.CENTER);
            thankYouLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
            mainPanel.add(thankYouLabel);
            mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));
        } else {
            // 未捐赠状态话术
            JLabel descLabel = new JLabel(languageManager.getString("dialog.donation.please"), SwingConstants.CENTER);
            descLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
            mainPanel.add(descLabel);
            mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));
        }
        
        // 添加二维码图片（如果存在）
        try {
            ImageIcon qrCodeIcon = new ImageIcon(getClass().getResource("/qr_code/qr_code.png"));
            Image img = qrCodeIcon.getImage();
            Image scaledImg = img.getScaledInstance(150, 150, Image.SCALE_SMOOTH);
            ImageIcon scaledIcon = new ImageIcon(scaledImg);
            JLabel qrCodeLabel = new JLabel(scaledIcon);
            qrCodeLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
            mainPanel.add(qrCodeLabel);
            mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));
        } catch (Exception e) {
            System.out.println("未找到捐赠二维码图片: " + e.getMessage());
            JLabel noImageLabel = new JLabel(languageManager.getString("dialog.donation.qr.error"));
            noImageLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
            mainPanel.add(noImageLabel);
            mainPanel.add(Box.createRigidArea(new Dimension(0, 20)));
        }
        
        // 创建按钮面板
        JPanel buttonPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
        buttonPanel.setAlignmentX(Component.CENTER_ALIGNMENT);
    
        closeButton = new JButton(languageManager.getString("dialog.donation.button.close"));
        closeButton.setPreferredSize(new Dimension(100, 30));
        buttonPanel.add(closeButton);

        donateButton = new JButton(languageManager.getString("dialog.donation.button.donated"));
        donateButton.setPreferredSize(new Dimension(100, 30));
        buttonPanel.add(donateButton);
    
        mainPanel.add(buttonPanel);
        add(mainPanel, BorderLayout.CENTER);
    }
    
    private void setupEventHandlers() {
        // 已捐赠按钮事件
        donateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                handleDonation();
            }
        });
        
        // 关闭按钮事件
        closeButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                donated = false;
                dispose(); // 立即关闭弹窗
            }
        });
    }
    
    private void handleDonation() {
        // 设置已捐赠状态并关闭弹窗
        appDataManager.setDonated(true);
        donated = true;
        dispose(); // 立即关闭弹窗
    }
    
    /**
     * 检查是否已捐赠
     * @return 是否已捐赠
     */
    public static boolean hasDonated() {
        return appDataManager.isDonated();
    }
    
    /**
     * 获取捐赠状态
     * @return 捐赠状态
     */
    public boolean isDonated() {
        return donated;
    }
}